<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Figures 6.21-6.23: Basis pursuit using Gabor functions</title>
<link rel="canonical" href="/Users/mcgrant/Repos/CVX/examples/cvxbook/Ch06_approx_fitting/html/basispursuit.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Figures 6.21-6.23: Basis pursuit using Gabor functions</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Section 6.5.4</span>
<span class="comment">% Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Original by Lieven Vandenberghe</span>
<span class="comment">% Adapted for CVX by Argyris Zymnis - 11/27/2005</span>
<span class="comment">%</span>
<span class="comment">% Here we find a sparse basis for a signal y out of</span>
<span class="comment">% a set of Gabor functions. We do this by solving</span>
<span class="comment">%       minimize  ||A*x-y||_2 + ||x||_1</span>
<span class="comment">%</span>
<span class="comment">% where the columns of A are sampled Gabor functions.</span>
<span class="comment">% We then fix the sparsity pattern obtained and solve</span>
<span class="comment">%       minimize  ||A*x-y||_2</span>
<span class="comment">%</span>
<span class="comment">% NOTE: The file takes a while to run</span>

clear

<span class="comment">% Problem parameters</span>
sigma = 0.05;  <span class="comment">% Size of Gaussian function</span>
Tinv  = 500;   <span class="comment">% Inverse of sample time</span>
Thr   = 0.001; <span class="comment">% Basis signal threshold</span>
kmax  = 30;    <span class="comment">% Number of signals are 2*kmax+1</span>
w0    = 5;     <span class="comment">% Base frequency (w0 * kmax should be 150 for good results)</span>

<span class="comment">% Build sine/cosine basis</span>
fprintf(1,<span class="string">'Building dictionary matrix...'</span>);
<span class="comment">% Gaussian kernels</span>
TK = (Tinv+1)*(2*kmax+1);
t  = (0:Tinv)'/Tinv;
A  = exp(-t.^2/(sigma^2));
ns = nnz(A&gt;=Thr)-1;
A  = A([ns+1:-1:1,2:ns+1],:);
ii = (0:2*ns)';
jj = ones(2*ns+1,1)*(1:Tinv+1);
oT = ones(1,Tinv+1);
A  = sparse(ii(:,oT)+jj,jj,A(:,oT));
A  = A(ns+1:ns+Tinv+1,:);
<span class="comment">% Sine/Cosine basis</span>
k  = [ 0, reshape( [ 1 : kmax ; 1 : kmax ], 1, 2 * kmax ) ];
p  = zeros(1,2*kmax+1); p(3:2:end) = -pi/2;
SC = cos(w0*t*k+ones(Tinv+1,1)*p);
<span class="comment">% Multiply</span>
ii = 1:numel(SC);
jj = rem(ii-1,Tinv+1)+1;
A  = sparse(ii,jj,SC(:)) * A;
A  = reshape(A,Tinv+1,(Tinv+1)*(2*kmax+1));
fprintf(1,<span class="string">'done.\n'</span>);

<span class="comment">% Construct example signal</span>
a = 0.5*sin(t*11)+1;
theta = sin(5*t)*30;
b = a.*sin(theta);

<span class="comment">% Solve the Basis Pursuit problem</span>
disp(<span class="string">'Solving Basis Pursuit problem...'</span>);
tic
cvx_begin
    variable <span class="string">x(30561)</span>
    minimize(sum_square(A*x-b)+norm(x,1))
cvx_end
disp(<span class="string">'done'</span>);
toc

<span class="comment">% Reoptimize problem over nonzero coefficients</span>
p = find(abs(x) &gt; 1e-5);
A2 = A(:,p);
x2 = A2 \ b;

<span class="comment">% Constants</span>
M = 61; <span class="comment">% Number of different Basis signals</span>
sk = 250; <span class="comment">% Index of s = 0.5</span>

<span class="comment">% Plot example basis functions;</span>
<span class="comment">%if (0) % to do this, re-run basispursuit.m to create A</span>
figure(1); clf;
subplot(3,1,1); plot(t,A(:,M*sk+1)); axis([0 1 -1 1]);
title(<span class="string">'Basis function 1'</span>);
subplot(3,1,2); plot(t,A(:,M*sk+31)); axis([0 1 -1 1]);
title(<span class="string">'Basis function 2'</span>);
subplot(3,1,3); plot(t,A(:,M*sk+61)); axis([0 1 -1 1]);
title(<span class="string">'Basis function 3'</span>);
<span class="comment">%print -deps bp-dict_helv.eps</span>

<span class="comment">% Plot reconstructed signal</span>
figure(2); clf;
subplot(2,1,1);
plot(t,A2*x2,<span class="string">'--'</span>,t,b,<span class="string">'-'</span>); axis([0 1 -1.5 1.5]);
xlabel(<span class="string">'t'</span>); ylabel(<span class="string">'y_{hat} and y'</span>);
title(<span class="string">'Original and Reconstructed signals'</span>)
subplot(2,1,2);
plot(t,A2*x2-b); axis([0 1 -0.06 0.06]);
title(<span class="string">'Reconstruction error'</span>)
xlabel(<span class="string">'t'</span>); ylabel(<span class="string">'y - y_{hat}'</span>);
<span class="comment">%print -deps bp-approx_helv.eps</span>

<span class="comment">% Plot frequency plot</span>
figure(3); clf;

subplot(2,1,1);
plot(t,b); xlabel(<span class="string">'t'</span>); ylabel(<span class="string">'y'</span>); axis([0 1 -1.5 1.5]);
title(<span class="string">'Original Signal'</span>)
subplot(2,1,2);
plot(t,150*abs(cos(w0*t)),<span class="string">'--'</span>);
hold <span class="string">on</span>;
<span class="keyword">for</span> k = 1:length(t);
  <span class="keyword">if</span>(abs(x((k-1)*M+1)) &gt; 1e-5), plot(t(k),0,<span class="string">'o'</span>); <span class="keyword">end</span>;
  <span class="keyword">for</span> j = 2:2:kmax*2
    <span class="keyword">if</span>((abs(x((k-1)*M+j)) &gt; 1e-5) | (abs(x((k-1)*M+j+1)) &gt; 1e-5)),
      plot(t(k),w0*j/2,<span class="string">'o'</span>);
    <span class="keyword">end</span>;
  <span class="keyword">end</span>;
<span class="keyword">end</span>;
xlabel(<span class="string">'t'</span>); ylabel(<span class="string">'w'</span>);
title(<span class="string">'Instantaneous frequency'</span>)
hold <span class="string">off</span>;
</pre>
<a id="output"></a>
<pre class="codeoutput">
Building dictionary matrix...done.
Solving Basis Pursuit problem...
 
Calling Mosek 9.1.9: 61625 variables, 502 equality constraints
------------------------------------------------------------

MOSEK Version 9.1.9 (Build date: 2019-11-21 11:32:15)
Copyright (c) MOSEK ApS, Denmark. WWW: mosek.com
Platform: MACOSX/64-X86

MOSEK warning 710: #1 (nearly) zero elements are specified in sparse col '' (505) of matrix 'A'.
MOSEK warning 710: #1 (nearly) zero elements are specified in sparse col '' (507) of matrix 'A'.
MOSEK warning 710: #1 (nearly) zero elements are specified in sparse col '' (509) of matrix 'A'.
MOSEK warning 710: #1 (nearly) zero elements are specified in sparse col '' (511) of matrix 'A'.
MOSEK warning 710: #1 (nearly) zero elements are specified in sparse col '' (513) of matrix 'A'.
MOSEK warning 710: #1 (nearly) zero elements are specified in sparse col '' (515) of matrix 'A'.
MOSEK warning 710: #1 (nearly) zero elements are specified in sparse col '' (517) of matrix 'A'.
MOSEK warning 710: #1 (nearly) zero elements are specified in sparse col '' (519) of matrix 'A'.
MOSEK warning 710: #1 (nearly) zero elements are specified in sparse col '' (521) of matrix 'A'.
MOSEK warning 710: #1 (nearly) zero elements are specified in sparse col '' (523) of matrix 'A'.
Warning number 710 is disabled.
Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 502             
  Cones                  : 30562           
  Scalar variables       : 61625           
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator - tries                  : 1                 time                   : 0.00            
Lin. dep.  - tries                  : 1                 time                   : 0.02            
Lin. dep.  - number                 : 0               
Presolve terminated. Time: 0.22    
Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 502             
  Cones                  : 30562           
  Scalar variables       : 61625           
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 502
Optimizer  - Cones                  : 30562
Optimizer  - Scalar variables       : 61625             conic                  : 61625           
Optimizer  - Semi-definite variables: 0                 scalarized             : 0               
Factor     - setup time             : 0.57              dense det. time        : 0.00            
Factor     - ML order time          : 0.00              GP order time          : 0.00            
Factor     - nonzeros before factor : 1.26e+05          after factor           : 1.26e+05        
Factor     - dense dim.             : 0                 flops                  : 9.84e+08        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   1.5e+00  1.0e+00  3.1e+04  0.00e+00   3.056200000e+04   0.000000000e+00   1.0e+00  1.16  
1   1.9e-01  1.3e-01  1.4e+03  1.00e+00   3.820389713e+03   8.384295397e-02   1.3e-01  1.28  
2   9.8e-03  6.6e-03  1.6e+01  1.00e+00   2.037854629e+02   3.417125965e+00   6.6e-03  1.43  
3   3.6e-03  2.4e-03  3.6e+00  1.00e+00   8.224218219e+01   8.938249410e+00   2.4e-03  1.53  
4   8.8e-04  5.9e-04  4.3e-01  1.00e+00   3.018097170e+01   1.216383545e+01   5.9e-04  1.68  
5   1.9e-04  1.3e-04  4.2e-02  1.00e+00   1.658929324e+01   1.272519117e+01   1.3e-04  1.82  
6   1.4e-04  9.4e-05  2.7e-02  1.00e+00   1.562401457e+01   1.275808457e+01   9.4e-05  1.91  
7   2.7e-05  1.8e-05  2.3e-03  1.00e+00   1.338149308e+01   1.283539099e+01   1.8e-05  2.06  
8   1.5e-05  1.0e-05  1.0e-03  1.00e+00   1.315475168e+01   1.284014261e+01   1.0e-05  2.15  
9   2.9e-06  2.0e-06  8.3e-05  1.00e+00   1.290468659e+01   1.284466544e+01   2.0e-06  2.29  
10  5.4e-07  3.6e-07  6.6e-06  1.00e+00   1.285608729e+01   1.284513132e+01   3.6e-07  2.43  
11  2.5e-07  1.7e-07  2.1e-06  1.00e+00   1.285033187e+01   1.284514396e+01   1.7e-07  2.52  
12  2.7e-09  1.8e-09  2.4e-09  1.00e+00   1.284521170e+01   1.284515561e+01   1.8e-09  2.66  
13  8.6e-11  6.5e-11  1.3e-11  1.00e+00   1.284515739e+01   1.284515569e+01   5.6e-11  2.76  
14  1.6e-10  2.2e-10  1.1e-15  1.00e+00   1.284515569e+01   1.284515569e+01   1.1e-13  2.86  
Optimizer terminated. Time: 2.92    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: 1.2845155690e+01    nrm: 1e+00    Viol.  con: 2e-10    var: 0e+00    cones: 0e+00  
  Dual.    obj: 1.2845155686e+01    nrm: 1e+00    Viol.  con: 0e+00    var: 2e-10    cones: 0e+00  
Optimizer summary
  Optimizer                 -                        time: 2.92    
    Interior-point          - iterations : 14        time: 2.91    
      Basis identification  -                        time: 0.00    
        Primal              - iterations : 0         time: 0.00    
        Dual                - iterations : 0         time: 0.00    
        Clean primal        - iterations : 0         time: 0.00    
        Clean dual          - iterations : 0         time: 0.00    
    Simplex                 -                        time: 0.00    
      Primal simplex        - iterations : 0         time: 0.00    
      Dual simplex          - iterations : 0         time: 0.00    
    Mixed integer           - relaxations: 0         time: 0.00    

------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +12.8452
 
done
Elapsed time is 11.172103 seconds.
</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="basispursuit__01.png" alt=""> <img src="basispursuit__02.png" alt=""> <img src="basispursuit__03.png" alt=""> 
</div>
</div>
</body>
</html>